package com.project.forlearningdemo.controller;

import com.project.forlearningdemo.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Api(value = "ResourceController", tags = {"与资源有关的API"})
@RestController
@RequestMapping("resource")
@CrossOrigin
public class ResourceController {

    @Resource
    ResourceService resourceServiceImpl;

    /**
     * 搜索资源
     */
    @ApiOperation("搜索资源")
    @PostMapping("query")
    public String queryResource(@ApiParam(value = "必须与条件对象中对应的字段 其中pageNum不能为空", required = true) @RequestBody String condition){
        return resourceServiceImpl.getResource(condition);
    }

    /**
     * 删除资源
     */
    @ApiOperation("删除资源 根据 userId, resourceId, status 删除")
    @PostMapping("delete")
    public String deleteResource(@ApiParam(value = "必须与条件对象中对应的字段", required = true)@RequestBody String condition){
        return resourceServiceImpl.deleteResource(condition);
    }

    /**
     * 增加资源
     */
    @ApiOperation("增加资源")
    @PostMapping("add")
    public String addResource(@ApiParam(value = "必须与资源对象中对应的字段 还要注意某些必填的字段", required = true) @RequestBody String resource){
        return resourceServiceImpl.addResource(resource);
    }

    /**
     * 修改资源
     */
    @ApiOperation("修改资源")
    @PostMapping("update")
    public String updateResource(@ApiParam(value = "必须与资源对象中对应的字段 还要注意某些必填的字段 根据ID修改", required = true) @RequestBody String resource){
        return resourceServiceImpl.updateResource(resource);
    }

    /**
     * 增加收藏
     */
    @ApiOperation("增加收藏资源")
    @PostMapping("/collection/{userId}/{resourceId}")
    public String addCollection(@ApiParam("收藏者编号")@PathVariable int userId,
                                @ApiParam("资源编号")@PathVariable int resourceId){
        return resourceServiceImpl.addCollection(resourceId, userId);
    }

    /**
     * 删除收藏
     */
    @ApiOperation("删除收藏资源")
    @DeleteMapping("/collection/{userId}/{resourceId}")
    public String deleteCollection(@ApiParam("收藏者编号")@PathVariable int userId,
                                   @ApiParam("资源编号")@PathVariable int resourceId){
        return resourceServiceImpl.deleteCollection(resourceId, userId);
    }

    /**
     * 查询收藏
     */
    @ApiOperation("查询收藏资源")
    @PostMapping("/collection")
    public String queryAllCollection(@ApiParam("可传入字段：当前页数 pageNum(必填) 收藏的资源编号 resourceId(非必填) 收藏者 userId(非必填) 标签 labels(非必填)") @RequestBody String collection){
        return resourceServiceImpl.queryCollection(collection);
    }

    /**
     * 增加浏览量
     */
    @ApiOperation("增加浏览量")
    @GetMapping("/addScanNum/{resourceId}")
    public String addScanNum(@ApiParam(value = "资源编号", required = true) @PathVariable int resourceId){
        return resourceServiceImpl.addScanNum(resourceId);
    }
}

